package ck.mmd.dao;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import ck.mmd.bean.Adopt;
import ck.mmd.util.DaoUtils;

public class AdoptDao {
	public void addAdopt(Adopt adopt) {
		String sql = "insert into adopt values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, adopt.getId(),adopt.getSenduser_id(),adopt.getRecvuser_id(),adopt.getDogtype_id(),adopt.getTitle(),
					adopt.getCoverimg(),adopt.getContent(),adopt.getHtml(),adopt.getImg_id(),adopt.getPetname(),adopt.getPetcolor(),
					adopt.getPetage(),adopt.getPetsex(),adopt.getIssend(),adopt.getIsfortrue(),adopt.getUptime(),adopt.getAddress(),
					adopt.getLooknum(),adopt.getReallooknum(),adopt.getRemarknum(),adopt.getStatus());
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public Adopt findAdoptById(String id) {
		String sql = "select * from adopt where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql,new BeanHandler<Adopt>(Adopt.class),id);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查询用户对应的发布的领养
	 * @param id
	 * @return
	 */
	public List<Adopt> findSendAdoptsById(String id) {
		String sql = "select * from adopt where senduser_id = ? and status = ? order by uptime desc";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql, new BeanListHandler<Adopt>(Adopt.class),id,1);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 查询用户对应接收的领养
	 * @param id
	 * @return
	 */
	public List<Adopt> findRecvAdoptsById(String id) {
		String sql = "select * from adopt where recvuser_id = ? and status = ? order by uptime desc";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql, new BeanListHandler<Adopt>(Adopt.class),id,1);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 删除id对应的领养信息(伪删除，修改属性)
	 * @param id
	 */
	public void deleteAdoptById(String id) {
		String sql = "update adopt set status =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, 0,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 查询最新的size个领养信息
	 * @param size
	 * @return
	 */
	public List<Adopt> findAdoptBySort(int size) {
		String sql = "select * from adopt where status = ? order by uptime desc limit ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			return runner.query(sql, new BeanListHandler<Adopt>(Adopt.class),1,size);
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 修改确认已认领
	 * @param id
	 */
	public void updateIsfortrue(String id,int state) {
		String sql = "update adopt set isfortrue =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, state,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 修改领养没有被申请
	 * @param id
	 * @param state
	 */
	public void updateIsSend(String id, int state) {
		String sql = "update adopt set issend =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, state,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void adaoupdateRecvUser(String id, String recvid) {
		String sql = "update adopt set recvuser_id =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, recvid,id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public void updateRemarkNum(String last_id, int i) {
		String sql = "update adopt set remarknum =? where id = ?";
		try{
			QueryRunner runner = new QueryRunner(DaoUtils.getSource());
			runner.update(sql, i,last_id);
		}catch(Exception e){
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}
